<template>
  <div v-enter-helper="getList" class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="primary" @click="exportExcel">导出</el-button>
          <el-button type="primary" @click="exportExcelAsync">
            异步导出
          </el-button>
          <el-form
            ref="listQuery"
            :inline="true"
            :model="listQuery"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-date-picker
                v-model="date"
                clearable
                end-placeholder="结束日期"
                range-separator="至"
                start-placeholder="开始日期"
                type="daterange"
                value-format="yyyy-MM-dd"
                @change="handleChangeData"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="listQuery.type"
                clearable
                filterable
                placeholder="请选择充值类型"
              >
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="listQuery.admin_id"
                clearable
                filterable
                placeholder="请选择管理员"
              >
                <el-option
                  v-for="item in adminOptions"
                  :key="item.admin_id"
                  :label="item.admin_name"
                  :value="item.admin_id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table
          ref="dataList"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width:100%;margin-bottom: 20px;"
        >
          <el-table-column label="用户名称" prop="nickname"></el-table-column>
          <el-table-column label="用户编号" prop="user_code"></el-table-column>
          <el-table-column label="用户头像" prop="nickname">
            <template slot-scope="{ row }">
              <img
                v-if="row.head_picture"
                :src="row.head_picture"
                alt=""
                style="width: 80px;height:80px;"
              />
            </template>
          </el-table-column>
          <el-table-column label="管理员名称" prop="admin_name">
          </el-table-column>
          <el-table-column label="充值金额" prop="gold"></el-table-column>
          <el-table-column label="充值类型" prop="type_name"></el-table-column>
          <el-table-column label="备注" prop="describe"></el-table-column>
          <el-table-column label="时间" prop="add_time"></el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { exportExcel, exportExcelAsync, getAdminList, listData } from '@/api/member/adminRecharge'

export default {
  data() {
    return {
      // 受限类型
      typeOptions: [
        { value: 1, label: '余额' },
        { value: 2, label: '收益' },
        { value: 3, label: '特殊币' }
      ],
      adminOptions: [],

      listLoading: false,
      menuList: [],
      date: null,
      listQuery: {
        page: 1,
        perPage: 10,
        start_time: undefined,
        end_time: undefined,
        type: undefined,
        admin_id: undefined
      },
      total: 0
    }
  },
  created() {
    this.getList()
    this.handleGetAdminList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listData(this.listQuery)
        .then(response => {
          this.menuList = response.data.list
          this.total = response.data.meta.total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleGetAdminList() {
      getAdminList().then(res => {
        this.adminOptions = res.data.list
      })
    },
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 点击重置
    resetSubmit() {
      this.listQuery.page = 1
      this.listQuery.perPage = 10
      this.listQuery.start_time = undefined
      this.listQuery.end_time = undefined
      this.listQuery.type = undefined
      this.listQuery.admin_id = undefined
    },
    exportExcel() {
      let obj = {}
      for (let key in this.listQuery) {
        if (key !== 'page' || key !== 'perPage') {
          obj[key] = this.listQuery[key]
        }
      }

      exportExcel(obj).then(response => {
        location.href = response.data.download_url
      })
    },
    exportExcelAsync() {
      let obj = {}
      for (let key in this.listQuery) {
        if (key !== 'page' && key !== 'perPage') {
          obj[key] = this.listQuery[key]
        }
      }

      exportExcelAsync(obj).then(response => {
        if (response.code === 200) {
          this.$message.success(
            '正在导出，请稍等！可移步到 订单管理-导出日志 查看下载链接'
          )
        }
      })
    },
    handleChangeData(value) {
      if (!value) {
        this.listQuery.start_time = undefined
        this.listQuery.end_time = undefined
      } else if (value && value.length === 2) {
        this.listQuery.start_time = value[0]
        this.listQuery.end_time = value[1]
      }
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

::v-deep .el-card__header {
  padding-bottom: 0;
}
</style>
